﻿#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include <QWidget>
#include <QPainter> // 实现窗口绘制的头文件
#include <QListWidget>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QEventLoop>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QDebug>
#include <QSqlDatabase>
#include <QSqlError>
#include <QTime>
#include <math.h>
#include <QMessageBox>
#include <QMouseEvent>
#include <QSqlQuery>
#include <QSqlRecord>

static QString kugouSearchApi = "http://mobilecdn.kugou.com/api/v3/search/song?";
static QString kugouDownloadApi = "https://www.kugou.com/yy/index.php?";


QT_BEGIN_NAMESPACE
namespace Ui { class MainWidget; }
QT_END_NAMESPACE

class MainWidget : public QWidget
{
    Q_OBJECT

public:
    MainWidget(QWidget *parent = nullptr);
    ~MainWidget();

    //  处理UI图片
    void paintEvent(QPaintEvent *event);

signals:
    void finish(QByteArray);
    void lyricshow(QString);

private slots:
    void on_pushButton_quit_clicked();

    void on_pushButton_about_clicked();

    void on_horizontalSlider_process_valueChanged(int value);

    void on_horizontalSlider_process_sliderPressed();

    void on_horizontalSlider_process_sliderReleased();

    void on_pushButton_last_clicked();

    void on_pushButton_Play_clicked();

    void on_pushButton_next_clicked();

    void on_pushButton_recycle_clicked();

    void on_horizontalSlider_valume_valueChanged(int value);

    void on_pushButton_search_clicked();

    // 更新播放进度条和显示的时间
    void updateDuration(qint64 value);

    // 读取网络数据
    void netReply(QNetworkReply *reply);

    // 显示歌词
    void lyricTextShow(QString lyric);

    // 播放音乐
    void playSearchMusic();
    void playHistoryMusic();

protected:
    // 音乐下载和播放
    void downloadAndPlay(QString album_id,QString hash);

    // 访问HTTP网页
    void httpAccess(QString url);

    // 音乐的hash和album_id解析,使用json
    void hashJsonAnalysis(QByteArray jsonData);

    // 搜索音乐数据信息解析
    QString musicJsonAnalysis(QByteArray jsonData);

private:
    Ui::MainWidget *ui;
    bool isPlay;
    QNetworkRequest *request;
    QNetworkAccessManager *manager;
    QMediaPlayer *player;
    QMediaPlaylist *playerList;
    QSqlDatabase db;
    int num,row;

private:
    // 处理鼠标拖动事件
    QPoint m_mousePoint;
    QPoint movePoint;
    bool isPress;


protected:
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mousePressEvent(QMouseEvent *event);
};
#endif // MAINWIDGET_H
